package com.mall.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.api.PmsProductApi;
import com.mall.db.api.SmsFlashPromotionProductRelationApi;
import com.mall.dto.FlashProductRelationUpdateDto;
import com.mall.pageResult.PageResult;
import com.mall.pojo.PmsProduct;
import com.mall.pojo.SmsFlashPromotionProductRelation;
import com.mall.vo.FlashProductRelationListVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FlashProductRelationService {

    @DubboReference
    private SmsFlashPromotionProductRelationApi relationApi;

    @DubboReference
    private PmsProductApi productApi;

    /**
     * 分页查询不同场次关联及商品信息
     *
     * @param pageNum
     * @param pageSize
     * @param flashPromotionId        活动id
     * @param flashPromotionSessionId 场次id
     * @return
     */
    public PageResult<FlashProductRelationListVo> list(Integer pageNum, Integer pageSize, Integer flashPromotionId, Integer flashPromotionSessionId) {
        //根据活动id和场次id查询商品分页信息
        Page<SmsFlashPromotionProductRelation> page = relationApi.findProductIds(pageNum, pageSize, flashPromotionId, flashPromotionSessionId);

        PageResult<FlashProductRelationListVo> result = new PageResult<>();
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setTotal(Convert.toInt(page.getTotal()));
        result.setTotalPage(Convert.toInt(page.getPages()));

        //判断productIds是否为空
        if (CollUtil.isEmpty(page.getRecords())) {
            return result;
        }
        //组装list数据
        List<FlashProductRelationListVo> list = new ArrayList<>();
        List<SmsFlashPromotionProductRelation> records = page.getRecords();
        list = records.stream().map(record -> {
            //根据商品id查询商品信息
            Long productId = record.getProductId();
            PmsProduct product = productApi.findById(productId);
            //组装vo
            FlashProductRelationListVo vo = new FlashProductRelationListVo();
            BeanUtils.copyProperties(record,vo);
            vo.setProduct(product);
            return vo;
        }).collect(Collectors.toList());

        result.setList(list);
        return result;
    }

    /**
     * 修改秒杀关联信息
     * @param relation
     * @return
     */
    public Integer update(FlashProductRelationUpdateDto relation) {
        //组装秒杀关联信息
        SmsFlashPromotionProductRelation flashPromotionProductRelation = new FlashProductRelationListVo();
        BeanUtils.copyProperties(relation,flashPromotionProductRelation);
        //修改秒杀关联信息
        Integer count = relationApi.update(flashPromotionProductRelation);
        return count;
    }

    /**
     * 批量选择商品添加关联 todo:其他接口为未现
     * @param relationList
     * @return
     */
    public Integer create(List<SmsFlashPromotionProductRelation> relationList) {
        Integer count = 0;
        //同一商品不能添加多次
        for (SmsFlashPromotionProductRelation relation : relationList) {
            Long flashPromotionId = relation.getFlashPromotionId();
            Long flashPromotionSessionId = relation.getFlashPromotionSessionId();
            Long productId = relation.getProductId();
            //通过三种id查询本场次秒杀活动与该商品的关联是否已存在
            SmsFlashPromotionProductRelation relation1 = relationApi.findRelation(flashPromotionId,flashPromotionSessionId,productId);
            if (ObjectUtil.isNull(relation1)){
                //不存在则保存关联关系
                relationApi.create(relation);
                count++;
            }
        }
        return count;
    }

    /**
     * 删除关联
     * @param id
     * @return
     */
    public Integer delete(Integer id) {
        Integer count = relationApi.delete(id);
        return count;
    }

}
